﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using OnlineVideos.Data;
using OnlineVideos.UI;
using Microsoft.Phone.Controls;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.Utilities;
using Common.Utilities;
using OnlineVideos.Entities;

namespace OnlineVideos
{
    public partial class FavoriteQuiz : UserControl
    {
        #region GlobalDeclaration
        List<QuizList> showFavSub = null;

        string link = string.Empty;
        private bool IsDataLoaded;
        #endregion

        public FavoriteQuiz()
        {
            InitializeComponent();
            IsDataLoaded = false;
            showFavSub = new List<QuizList>();

        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsDataLoaded == false)
            {
                GetPageDataInBackground();
                IsDataLoaded = true;
                PageHelper.RemoveEntryFromBackStack("FavoriteQuiz");
            }

        }
        private void Quizremovefromfavorites_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (lbxFavoritesubjects == null)
                    return;
                ListBoxItem selectedListBoxItem = this.lbxFavoritesubjects.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;

                FavoritesManager.QuizAddOrRemoveFavorite((selectedListBoxItem.Content as QuizList).ShowID, (selectedListBoxItem.Content as QuizList).QuizID);

                PageHelper.NavigateTo(NavigationHelper.MainPanoramaPage);

            }
            catch (Exception ex)
            {
                string mess = "Exception in examremovefromfavorites_Click Method In Favorites file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                           (object s, DoWorkEventArgs a) =>
                                           {
                                               a.Result = FavoritesManager.GetFavoritequiz();
                                           },
                                           (object s, RunWorkerCompletedEventArgs a) =>
                                           {


                                               showFavSub = (List<QuizList>)a.Result;
                                               if (showFavSub.Count > 0)
                                               {
                                                   lbxFavoritesubjects.ItemsSource = showFavSub;
                                                   tblkFavNosubjects.Visibility = Visibility.Collapsed;
                                               }
                                               else
                                               {
                                                   tblkFavNosubjects.Text = "No " + AppResources.ShowQuizPivotTitle + " available";
                                                   tblkFavNosubjects.Visibility = Visibility.Visible;
                                               }
                                           }
                                         );


            bwHelper.RunBackgroundWorkers();
        }

        private void lbxFavoritesubjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxFavoritesubjects.SelectedIndex == -1)
                    return;
                AppState.searchtitle = (lbxFavoritesubjects.SelectedItem as QuizList).Name;
                PageHelper.NavigateTo(NavigationHelper.getSubjectDetailPage((lbxFavoritesubjects.SelectedItem as QuizList), "true"));
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxMovies_SelectionChanged Method In Favorites file.", ex);
            }
        }

    }
}

